/**
 * Created by lixiaodi on 2019/3/4 0027.
 */
mini.parse();
var grid = mini.get("datagrid1");
var editBtn = mini.get('editBtn');
var removeBtn = mini.get('removeBtn');
var url = bootPATH + "../frame/frame1/pages/";      	//路径前缀
//初始化加载数据
$(function(){
	//表头添加菜单按钮
	var menu = new ColumnsMenu(grid);
	//请求数据
	httpRequest();
});
//选中行事件
function selectEvent(){
	var row = grid.getSelected();
	if(row.btnIsPreset == '是'){
		editBtn.disable();
		removeBtn.disable();
	}else{
		editBtn.enable();
		removeBtn.enable();
	}
}
//增加
function add() {
    mini.open({
        url: url +"alertBox/btnWindow.html",
        title: "新增按钮", width: 600, height: 400,
        onload: function () {
            var iframe = this.getIFrameEl();
            var data = { action: "new"};
            iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
        	httpRequest();
        }
    });
}
//编辑
function edit() {
    var row = grid.getSelected();
    if (row) {
        mini.open({
            url: url +"alertBox/btnWindow.html",
            title: "编辑按钮", width: 600, height: 400,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { action: "edit", id: row.buttonId };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
            	httpRequest();
            }
        });
    } else {
        mini.alert("请选中一条记录");
    }
}
//删除
function remove() {
    var rows = grid.getSelecteds();
    if (rows.length > 0) {
        mini.confirm("确定删除选中记录？","系统提示",function(action){
            if (action == "ok") {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.buttonId);
                }
                var id = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: ajaxUrl + "/buttons/deleteButtons.do?buttonId=" + id,
                    type:"post",
                    success: function (text) {
                    	httpRequest();
                    },
                    error: function () {
                        mini.alert('失败');
                    }
                });
            }
        })
    }else {
        mini.alert("请选中一条记录");
    }
}
//顺序调整：向上一行
function topChange(){
	var topRow = grid.getSelected();
	if(topRow){
		var btnIdVal = topRow.buttonId;
		$.ajax({
			url  : ajaxUrl + '/buttons/lastButtons.do',
			type : 'post',
			data : {
				buttonsId : btnIdVal
			},
			success : function(data) {
				if (data.state == 'success') {
				    //alert("888")
					httpRequest(topRow);
				}else{
					mini.alert(data.message);
				} 
			}
		})
	}else{
		mini.alert('请选择需要移动的行');
	}
}

//顺序调整：向下一行
/*function  bottonChange() {
    var row=grid.getSelected();
    if(row){
        var index=grid.indexOf(row);
        console.log(index);
        grid.moveRow(row,index-1);
        // httpRequest();
    }

}*/

//顺序调整：向下一行
function bottonChange(){
	var bottomRow = grid.getSelected();
	console.log(bottomRow);
	if(bottomRow){
		var btnIdVal = bottomRow.buttonId;
		$.ajax({
			url  : ajaxUrl + '/buttons/nextButtons.do',
			type : 'post',
			data : {
				buttonsId : btnIdVal
			},
			success : function(data){
				if(data.state == 'success'){
					//加载完成后默认选中勾选的行
					httpRequest(bottomRow);
                    //alert("8889")
				}else{
					mini.alert(data.message);
				}
			},
			error : function(message){
				mini.alert(message);
			}
		})
	}else{
		mini.alert('请选择需要移动的行');
	}
}
//查询
function search() {
    var key = mini.get("key").getValue();
    $.ajax({
        url: ajaxUrl + "/buttons/findById.do",
        type:"post",
        datatype:"json",
        data:{
            departmentName:key
        },
        success: function (data) {
            grid.setData(data.data)
            //grid.reload();
        },
        error: function () {
            mini.alert("失败");
        }
    });
}
function onKeyEnter(e) {
    search();
}

//刷新
function refresh(){
	httpRequest();
}
//数据请求
function httpRequest(Row){
	$.ajax({
        url: ajaxUrl + "/buttons/list.do",
        type:"post",
        success: function (data) {
        	var btnData = data.data;
        	console.log(btnData);
        	for(var i = 0; i < btnData.length; i++){
        		//按钮状态转化
        		if(btnData[i].state){
        			btnData[i].state = btnData[i].state == 1 ? '已开启' : '已关闭';
        		}else{
        			btnData[i].state = '';
        		}
        		//是否系统预制
        		btnData[i].btnIsPreset = btnData[i].btnIsPreset == 1 ? '是' : '否';
        	}
        	grid.setData(btnData);
        	if(Row){
        		grid.select(Row,true);
        	}else{

        	}
        },
        error:function(data){
            mini.alert('请求失败');
        }
    });
}
